package com.nantai.jiafang.nantaijiafang.nt5s.Activity;


import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.hzc.imagepicker.activity.PhotoPickerActivity;
import com.nantai.jiafang.nantaijiafang.Data;
import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.databinding.ActivityUpErrorBinding;
import com.nantai.jiafang.nantaijiafang.nt5s.adapter.SectionsPagerAdapter;
import com.nantai.jiafang.nantaijiafang.nt5s.fragments.UpErrorfragment1;
import com.nantai.jiafang.nantaijiafang.nt5s.fragments.UpErrorfragment2;
import com.nantai.jiafang.nantaijiafang.nt5s.fragments.UpErrorfragment_Add;

import org.zackratos.ultimatebar.UltimateBar;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class UperrorActivity extends AppCompatActivity {
  private  ActivityUpErrorBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         binding= DataBindingUtil.setContentView(this,R.layout.activity_up_error);
        UltimateBar ultimateBar=new UltimateBar(this);
        ultimateBar.setColorBar(getResources().getColor(R.color.uperror),1);
        final Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle(R.string.uperror);
        setSupportActionBar(toolbar);
        ActionBar actionBar=getSupportActionBar();
        if (actionBar!=null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_general_back);
            actionBar.setHomeButtonEnabled(true);
        }
        List<Fragment> fragmentList=new ArrayList<>();
        fragmentList.add(new UpErrorfragment1());
        fragmentList.add(new UpErrorfragment2());
        PagerAdapter pagerAdapter= new SectionsPagerAdapter(this,getSupportFragmentManager(),fragmentList);
        binding.viewpager.setAdapter(pagerAdapter);
        binding.tabHeard.setViewPager(binding.viewpager);
        binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Data.Selectfragment=position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            final ArrayList<String> pathList =
                    data.getStringArrayListExtra(PhotoPickerActivity.EXTRA_RESULT_SELECTION);
            System.out.println("pathList" + pathList.toString());
            //Toast.makeText(this, String.valueOf(pathList.size()), Toast.LENGTH_SHORT).show();
            UpErrorfragment_Add upErrorfragment_add=(UpErrorfragment_Add)getSupportFragmentManager().findFragmentByTag("fragment1");
            upErrorfragment_add.refresh(pathList);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (Data.netpiclist!=null&&Data.netpiclist.size()!=0) {
            if (Data.netpiclist.size() != 0) {
                for (String path :
                        Data.netpiclist) {
                    new File(path).delete();
                }
                Data.netpiclist.clear();
            }
        }
    }
}
